package cloud.seata.warehouse.service;

import cloud.seata.api.TccAccountService;
import cloud.seata.api.TccWarehouseService;
import cloud.seata.warehouse.repository.WarehouseRopository;
import cloud.seata.warehouse.repository.entity.Stockpile;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.seata.rm.tcc.api.BusinessActionContext;
import org.apache.seata.rm.tcc.api.BusinessActionContextParameter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;


@DubboService
@Service
public class WarehouseServiceImpl implements TccWarehouseService {

    private static final Logger log = LoggerFactory.getLogger(WarehouseServiceImpl.class);
    @Autowired
    private WarehouseRopository warehouseRopository;

    @Override
    public boolean prepare(BusinessActionContext actionContext,Long productId,
                           Integer frozen) throws Exception {
        Stockpile stockpile = warehouseRopository.selectById(1);

        log.info("产品Id：{} 和冻结库存数量：{}", productId.toString(), frozen.toString());

        Stockpile wh = new Stockpile();
        wh.setId(1);
        wh.setAmount(stockpile.getAmount()-9);
        wh.setFrozen(stockpile.getFrozen()+9);
        wh.setProduct_id(1);
        warehouseRopository.updateById(wh);
//        throw new Exception("库存不足");
        return true;
    }

    @Override
    public boolean commit(BusinessActionContext actionContext) {

        Stockpile stockpile = warehouseRopository.selectById(1);
        System.out.println("阿喀琉斯的感觉拉开圣诞节个垃圾阿三顶顶发动反攻阿文发生的是阿斯利康国家拉动可是结果");

        Long productId = actionContext.getActionContext("productId", Long.class);
        Integer frozen = actionContext.getActionContext("frozen", Integer.class);
        log.info("commit 的 {}， 和{}", productId, frozen);
        stockpile.setFrozen(stockpile.getFrozen()-9);
        warehouseRopository.updateById(stockpile);
        return true;
    }

    @Override
    public boolean rollback(BusinessActionContext actionContext) {
        Stockpile stockpile = warehouseRopository.selectById(1);
        System.out.println("阿喀琉斯的感觉拉开圣诞节个垃圾是阿斯利康国家拉动可是结果");
        stockpile.setFrozen(stockpile.getFrozen()-9);
        stockpile.setAmount(stockpile.getAmount()+9);
        warehouseRopository.updateById(stockpile);
        return true;
    }
}
